<?php

namespace App\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Models\Order;

class MakeMoney extends Model
{
    protected $table = "c_make_moneys_log";

    /**
     * @param $data
     * @return bool|string
     * 余额支付
     */
    public function doSurplusPay($data)
    {
        try {
            DB::beginTransaction(); // 开始事务
            $orderClass = new Order();
            $userClass = new User();
            $doctorWorksClass = new DoctorWorks();
            if ($data['types'] == 1 || $data['types'] == 2) { //2商品和1课程
                $order = $orderClass::getOrderDetail($data['order_id']);
                if (!$order) {
                    return "该订单不存在!";
                }
                if ($order->pay_status == 1) {
                    return "该订单已经支付,不能重复支付!";
                }
                $logs = $this->getLogsInfo($data);
                if ($logs) {
                    return "该订单已经有付款记录,不能重复支付!";
                }
                $order->pay_type = 3;
                $order->pay_status = 1;
                $order->pay_time = date("Y-m-d H:i:s", time());
                //查询用户余额
                $users = $userClass->where([['id', '=', $data['user_id']]])->first();
                if ($order->pay_price > $users->surplus) {
                    return "您的余额不足,请先充值再付!";
                }
                $this->type = 3;
                $this->money = $order->pay_price;
                $this->sn = $orderClass->orderNumber();
                $this->order_id = $data['order_id'];
                $this->pay_type = 3;
                $this->user_id = $data['user_id'];
                if ($order->order_type == 1) {
                    $this->remarks = "购买课程订单号" . $order->sn;
                } else {
                    $this->remarks = "购买商品订单号" . $order->sn;
                }
                $this->add_sub = 2;
                $this->work_id = $order->work_id;
                $this->content_type = $data['types'];
                $this->instime = date("Y-m-d H:i:s", time());
                $makeMoneyLog = $this->save();
                if ($makeMoneyLog) {
                    $u_money = $userClass->where([['id', '=', $data['user_id']]])->decrement('surplus', $order->pay_price); //用户余额
                    $up_order = $order->save(); //修改订单状态
                    $doctorWorksClass->where([['id', '=', $order->work_id]])->increment('surplus', $order->pay_price); //工作室加余额
                    if ($u_money && $up_order) {
                        DB::commit();
                        return true;
                    } else {
                        DB::rollback(); // 回滚事务
                        return "支付失败";
                    }
                }
            } elseif ($data['types'] == 3) {  //3打赏
                $rewardClass = new Reward();
                $order = $rewardClass::getRewardDetail($data['order_id']);
                if (!$order) {
                    return "该订单不存在!";
                }
                if ($order->pay_status == 1) {
                    return "该订单已经支付,不能重复支付!";
                }
                $logs = $this->getLogsInfo($data);
                if ($logs) {
                    return "该订单已经有付款记录,不能重复支付!";
                }
                $order->pay_type = 3;
                $order->pay_status = 1;
                $order->pay_time = date("Y-m-d H:i:s", time());
                //查询用户余额
                $users = $userClass->where([['id', '=', $data['user_id']]])->first();
                if ($order->money > $users->surplus) {
                    return "您的余额不足,请先充值再付!";
                }
                $work_id = self::getWorkId($order->doctor_id);
                if (empty($work_id)) {
                    return "该用户已被工作室解除,不能打赏!";
                }
                $this->type = 3;
                $this->money = $order->money;
                $this->sn = $orderClass->orderNumber();
                $this->order_id = $data['order_id'];
                $this->pay_type = 3;
                $this->user_id = $data['user_id'];
                $this->remarks = "医师打赏";
                $this->add_sub = 2;
                $this->work_id = $work_id;
                $this->content_type = $data['types'];
                $this->instime = date("Y-m-d H:i:s", time());
                $makeMoneyLog = $this->save();
                if ($makeMoneyLog) {
                    $u_money = $userClass->where([['id', '=', $data['user_id']]])->decrement('surplus', $order->money); //用户余额
                    $up_order = $order->save(); //修改订单状态
                    $doctorWorksClass->where([['id', '=', $work_id]])->increment('surplus', $order->money); //工作室加余额
                    if ($u_money && $up_order) {
                        DB::commit();
                        return true;
                    } else {
                        DB::rollback(); // 回滚事务
                        return "支付失败";
                    }
                }
            } elseif ($data['types'] == 4) {   //4咨询
                $appointmentClass = new Appointment();
                $order = $appointmentClass->getInfo($data['order_id']);
                if (!$order) {
                    return "该订单不存在!";
                }
                if ($order->pay_status == 1) {
                    return "该订单已经支付,不能重复支付!";
                }
                $logs = $this->getLogsInfo($data);
                if ($logs) {
                    return "该订单已经有付款记录,不能重复支付!";
                }
                $order->pay_type = 3;
                $order->pay_status = 1;
                $order->pay_time = date("Y-m-d H:i:s", time());
                //查询用户余额
                $users = $userClass->where([['id', '=', $data['user_id']]])->first();
                if ($order->money > $users->surplus) {
                    return "您的余额不足,请先充值再付!";
                }
                $work_id = self::getWorkId($order->doc_id);
                if (empty($work_id)) {
                    return "该用户已被工作室解除,不能打赏!";
                }
                $this->type = 3;
                $this->money = $order->price;
                $this->sn = $orderClass->orderNumber();
                $this->order_id = $data['order_id'];
                $this->pay_type = 3;
                $this->user_id = $data['user_id'];
                $this->remarks = "医师咨询";
                $this->add_sub = 2;
                $this->work_id = $work_id;
                $this->content_type = $data['types'];
                $this->instime = date("Y-m-d H:i:s", time());
                $makeMoneyLog = $this->save();
                if ($makeMoneyLog) {
                    $u_money = $userClass->where([['id', '=', $data['user_id']]])->decrement('surplus', $order->price); //用户余额
                    $up_order = $order->save(); //修改订单状态
                    $doctorWorksClass->where([['id', '=', $work_id]])->increment('surplus', $order->price); //工作室加余额
                    if ($u_money && $up_order) {
                        DB::commit();
                        return true;
                    } else {
                        DB::rollback(); // 回滚事务
                        return "支付失败";
                    }
                }
            }
        } catch (Exception $e) {
            DB::rollback(); // 回滚事务
        }
    }

    /***
     * @param $data
     * @return mixed
     * 查看是否已有记录
     */
    public function getLogsInfo($data)
    {
        return $this->where([['order_id', '=', $data['order_id']], ['user_id', '=', $data['user_id']], ['content_type', '=', $data['types']]])->first();
    }

    /***
     * @param $data
     * @return mixed
     * 获取工作室ID
     */
    public static function getWorkId($doctor_id)
    {
        return (new Doctor())->where([['id', '=', $doctor_id]])->value("work_id");
    }

    /***
     * @param $param
     * 充值
     */
    public function recharge($param)
    {
        $rechargeWithdrawal = new RechargeWithdrawal();
        $rechargeWithdrawal->money = $param['money'];
        $rechargeWithdrawal->user_id = $param['user_id'];
        $rechargeWithdrawal->sn = (new \App\Models\Order())->orderNumber();
        //微信充值
        if ($param['type'] == 1) {
            $rechargeWithdrawal->pay_type = 1;
            $res = $rechargeWithdrawal->save();
            if ($res) {

            }
        } elseif ($param['type'] == 2) {   //支付宝充值
            $rechargeWithdrawal->pay_type = 2;
            $res = $rechargeWithdrawal->save();
            if ($res) {

            }
        }
    }
}
